<?php

/**
 * BKloud Framework
 * Core_Router class
 * 
 * LICENSE:
 * BKloud Framework duoc xay dung va phat trien boi nhom BKloud - dai hoc bach khoa Ha Noi 
 * Ban duoc phep su dung framework nay cho muc dich hoc tap , nghien cuu va cac ung dung phi thuong mai
 * 
 * @copyright Copyright (c) 2011 BKloud - HPCC.
 * @version 0.1
 */

class Core_Router
{
    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
    }
    /**
     * Thuc hien goi phuong thuc action trong doi tuong cua controller
     * Hien thi ket qua tu template
     *
     * @return void
     */
    public function router()
    {
        $registry = Core_Registry::singleton();
        $http     = $registry['http'];
        $module   = $http->getModule();
        $controller = $http->getController();
        $action     = $http->getAction();

        $file = APPLICATION_PATH . '/modules/' . $module . '/controllers/' . $controller .'.php';
        if (!is_readable($file))
            throw new Exception('Unable to load controller: ' . $file, 404);
        include_once ($file);

        $objController = new $controller();

        if (!is_callable(array($objController, $action)))
            throw new Exception('Unable to call action: ' . $action . ' in file ' . $file,404);

        $objController->$action();

        $registry['template']->show();
    }
}
